package com.example.TestCode.JavaBasics;

/**
 * @auther hewenhui
 * @see <a href="hetp://www.codingmaster.com> java </a>
 */
class Person {
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class demo_4_20 {

    public static void main(String[] args) {
        Person actual = new Person("zxx", 21);
        System.out.println("修改前：" + actual.toString());
        fun(actual); // actual实参，引用类型数据
        System.out.println("修改后：" + actual.toString());

    }

    public static void fun(Person formal) { // formal形参，和实参actual指向同一个对象
        formal.name = "hyf";// 因为formal和actual指向同一个对象，所以修改formal，actual也会改变
        formal = new Person("zw", 22); // 创建了一个新的对象，formal和actual不在指向同一个对象，此时修改形参不会影响实参
        // 上面是显式地创建对象，有时候会隐式地创建对象：
        String s = new String("abc");
        System.out.println("s1:"+System.identityHashCode(s));
        s = "abc"; // 隐式地创建了新对象
        System.out.println("s2:"+System.identityHashCode(s));
        Integer i = new Integer(10);
        System.out.println("i1:"+System.identityHashCode(i));
        i = 20; // 隐式地创建了新对象
        System.out.println("i2:"+System.identityHashCode(i));
        // identityHashCode的值前后不同，说明隐式创建了新的对象
    }
}
